<?php
	/**
	 * 文件上传以及图片处理类
	 * 包括文件上传，生成所略图，添加水印
	 *
	 * 参数$file 为上传文件的数组$_FILES['upload']
	 * */

	class UpFile{

		//上传文件保存目录
		private $uploadDir;

		//文件类型
		private $fileType;

		//上传文件$_FILES数组
		private $fileInfo;

		//生成缩略图以及添加水印的一些私有属性
		private $srcWidth,$srcHeight,$img,$dst,$image2smallResult,$waterMarkImg,$waterMarkWidth,$waterMarkHeight,$imagecopymergeResult;

		//文件上传之后的路径
		public $newPath;

		//获取上传文件目录
		function __construct($uploadDir){
			return $this->uploadDir = $uploadDir;
		}

		//检查文件类型
		function checkType($file){
			$this->fileInfo = $file;
			$this->fileType = array('image/pjpeg','image/jpeg','image/gif','image/png');
			return in_array($file['type'],$this->fileType)?true:false;
		}

		//移动文件到上传目录
		function moveUpfile($file){
			$this->ext = pathinfo($file['name']);
			$this->newPath = $this->uploadDir.time().'.'.$this->ext['extension'];
			return move_uploaded_file($file['tmp_name'],$this->newPath);
		}

		//生成缩略图
		/**
         *$file_name : 要生成的文件名称
		 *$file_path : 要生成的源图片文件路径
		 * $file_thumb_dir : 生成的缩略图存放路径
		 */
		function image2small($file_path,$new_file_name,$file_thumb_dir='./upload/thumb/',$destinationWidth='100',$destinationHeight='120'){
			$data = getimagesize($file_path);
            $str = explode($file_path,'/');
			switch ($data[2]){
				case 1:
					$this->img=imagecreatefromgif($file_path);
					break;
				case 2:
					$this->img=imagecreatefromjpeg($file_path);
					break;
				case 3:
					$this->img=imagecreatefrompng($file_path);
					break;
			}
			$this->srcWidth=imagesx($this->img);
			$this->srcHeight=imagesy($this->img);
			$this->dst=imagecreatetruecolor($destinationWidth,$destinationHeight);
			$this->image2smallResult = imagecopyresampled($this->dst,$this->img,0,0,0,0,$destinationWidth,$destinationHeight,$this->srcWidth,$this->srcHeight);
			switch ($data[2]){
				case 1:
					imagegif($this->dst,$file_thumb_dir.$new_file_name);
					break;
				case 2:
					imagejpeg($this->dst,$file_thumb_dir.$new_file_name);
					break;
				case 3:
					imagepng($this->dst,$file_thumb_dir.$new_file_name);
					break;
			}
			imagedestroy($this->img);
			imagedestroy($this->dst);
			return $this->image2smallResult;
		}

		//给图片加水印
		//$image 要加水印的图片，$waterMarkImg 水印图片
		function makeWaterMark($image,$waterMarkImg){
			$data = getimagesize($waterMarkImg);
			switch ($data[2]){
				case 1:
                    $this->waterMarkImg = imagecreatefromgif($waterMarkImg);
					$img=imagecreatefromgif($image);
					break;
				case 2:
                    $this->waterMarkImg = imagecreatefromjpeg($waterMarkImg);
					$img=imagecreatefromjpeg($image);
					break;
				case 3:
                    $this->waterMarkImg = imagecreatefrompng($waterMarkImg);
					$img=imagecreatefrompng($image);
					break;
			}
           	$this->waterMarkWidth = imagesx($this->waterMarkImg);
			$this->waterMarkHeight = imagesy($this->waterMarkImg);
			$imgSize = getimagesize($image);
			$dstX = $imgSize[0]-$this->waterMarkWidth;
			$dstY = $imgSize[1]-$this->waterMarkHeight;
            //下面这个函数是起主要作用的。。
			$this->imagecopymergeResult = imagecopymerge($img,$this->waterMarkImg,$dstX,$dstY,0,0,$this->waterMarkWidth,$this->waterMarkHeight,100);
			switch ($data[2]){
				case 1:
					imagegif($img,$this->newPath);
					break;
				case 2:
					imagejpeg($img,$this->newPath);
					break;
				case 3:
					imagepng($img,$this->newPath);
					break;
			}
			imagedestroy($img);
			imagedestroy($this->waterMarkImg);
			return $this->imagecopymergeResult;
		}
	}
?>